package common.net
{

    import common.display.ActionImage;

    import flash.display.BitmapData;
    import flash.events.Event;
    import flash.events.TimerEvent;
    import flash.net.URLRequest;
    import flash.utils.Timer;

    import kernel.cdn.CDN;
    import kernel.eventCenter.EventCenter;

    import map.events.MemoryEvent;

    import org.granite.net.AbstractAssetReferenceManager;
    import org.granite.net.ILoader;
    import org.granite.net.LoaderConfiguration;
    import org.granite.net.LoaderQueue;
    import org.granite.util.ReferenceManager;



    public class URLActionImagesManager extends AbstractAssetReferenceManager
    {
        public static const instance:URLActionImagesManager = new URLActionImagesManager ();
        private var _temp:Object;
        private var _timer:Timer;

        public function URLActionImagesManager():void
        {
            super (new LoaderQueue (2 , 40) , ReferenceManager.COUNT_REFERENCE);
            _temp = {};
            _timer = new Timer (60000);
            _timer.addEventListener (TimerEvent.TIMER , _timerHandler , false , 0 , true);
            _timer.start ();
            EventCenter.addEventListener (MemoryEvent.MAP_CHANGE_DISPOSE , disposeMem);
        }

        private function disposeMem(event:Event):void
        {
            _timerHandler ();
        }

        public override function createLoaderConfiguration(path:String):LoaderConfiguration
        {
            var lconfig:LoaderConfiguration = new LoaderConfiguration ();
            lconfig.loader = new ActionImageLoader ();
            lconfig.request = CDN.creatNewURLRequest (path);
            return lconfig;
        }

        public function hasDataWithTemp(url:String):Boolean
        {
            return _dataMap[url] != null || _temp[url];
        }

        public override function reference(url:String , key:* = null , defaultData:* = null):void
        {
            var data:* = _dataMap[url];
            if (data == null)
            {
                data = _temp[url];
                if (data != null)
                {
                    _dataMap[url] = data;
                    delete _temp[url];
                }
            }
            super.reference (url , key);
        }

        protected override function _destroyData(url:String , data:* , error:Boolean):void
        {
            var temp:* = _temp[url];
            if (temp != null)
            {
                (temp as ActionImage).destory ();
            }
            if (!error)
                _temp[url] = data;
        }

        protected override function _getLoadedData(path:String , loader:ILoader , error:Boolean):*
        {
            return (loader as ActionImageLoader).createActionImage ();
        }

        private function _timerHandler(e:Event = null):void
        {
            for each (var data:* in _temp)
            {
                data.destory ();
            }
            _temp = {};
        }
    }
}
